
(function(window){
	'use strict'
	var doc = window.document;
	// var o = doc.body;
	
	/**
	 * 五子棋游戏
	 */
	var Game = {
		createNew: function(target){
			var game = {};
			game.target = target;
			game.steps = new Array();
			game.turn = Chessman.BLACK;		//黑棋先手
			game.winner = null;
			game.players = new Array();	
			
			game.start = function(){
				var $this = this;
				initPlayer($this);
				$this.chessboard.element.onclick = function(e){
					var id = e.target.id;
					if(id){
						var arr = id.split(',');
						var position = {x:parseInt(arr[0]),y:parseInt(arr[1])};
						var player = $this.players[$this.turn];
						player.down(position);
						if(player.stepOver){
							$this.doTurn();
						}
					}
				}
			}
			game.end = function(){
				this.chessboard.clean(this);
				this.steps[Chessman.BLACK] = [];
				this.steps[Chessman.WHITE] = [];
				this.turn = Chessman.BLACK;
				this.winner = null;
				initMessage(this);
			}
			game.doTurn = function(){
				if(this.turn == Chessman.BLACK){
					this.turn = Chessman.WHITE;
				}else{
					this.turn = Chessman.BLACK;
				}
				initMessage(this);
			}
			game.admit = function(){
				this.players[this.turn].admit();
			}
			game.regret = function(){
				var target = this.turn==Chessman.BLACK?Chessman.WHITE:Chessman.BLACK;
				this.players[target].regret();
				this.doTurn();
			}
			game.notice = function(player,isWin){
				var $this = this;
				if(isWin){
					$this.winner = player;
				}else{
					$this.players.forEach(function(p){
						if(p.color != player){
							$this.winner = p;
						}
					});
				}
				alert(this.winner.color + "胜");
				this.end();
			}
			game.message = function(str){
				var msg = doc.getElementById('msg');
				msg.innerHTML = str;
			}
			
			initChessboard(game);
			
			return game;
		}
	}
	
	/**
	 * 初始化棋盘
	 */
	function initChessboard(game){
		game.chessboard = new Chessboard(game.target);
		initMessage(game);
	}
	
	function initMessage(game){
		game.chessboard.setTurn(game.turn);
		game.message("该"+(game.turn==Chessman.BLACK?"黑棋":"白棋")+"落子")
	}
	
	function initPlayer(game){
		game.players.push(new Player(game,Chessman.BLACK,false));
		game.players.push(new Player(game,Chessman.WHITE,false));
		game.steps.push(new Array);
		game.steps.push(new Array);
	}
	
	/**
	 * 棋盘
	 */
	function Chessboard(target){
		if(target){
			this.element = document.getElementById(target);
			this.drawChessboard();
		}else{
			console.error("does not have any target!");
		}
	}
	
	Chessboard.prototype = {
		drawChessboard: function(){
			var size = 19;
			var cellWidth = 24;
			var rowArr = new Array();
			for(var i=0;i<size;i++){
				var cellArr = new Array();
				for(var j=0;j<size;j++){
					var cell = document.createElement('div');
					cell.className = 'cell';
					cell.style.width = cellWidth +"px";
					cell.style.height = cellWidth + "px";
					cell.id = (j+1)+","+(i+1);
					this.element.appendChild(cell);
					cellArr.push(Chessman.NULL);
				}
				rowArr.push(cellArr);
			}
			this.matrix = rowArr;
		},
		clean: function(game){
			var $this = this;
			game.steps.forEach(function(step){
				step.forEach(function(position){
					$this.removeCell(position);
				})
			});
		},
		downCell: function(position,color){
			var value = this.matrix[position.x][position.y] ;
			var result = false;
			if(value == Chessman.NULL){
				this.matrix[position.x][position.y] = color;
				var chessman = doc.createElement('div');
				var className = 'chessman';
				switch(color){
					case Chessman.BLACK: className += " black";
									break;
					case Chessman.WHITE:  className += " white";
									break;
					default:break;								
				}
				chessman.className = className;
				var cell = doc.getElementById(position.x+","+position.y);
				cell.appendChild(chessman);
				result = true;
			}
			return result;
		},
		removeCell: function(position){
			this.matrix[position.x][position.y] = Chessman.NULL;
			var cell = doc.getElementById(position.x+","+position.y);
			cell.removeChild(cell.childNodes[0]);
		},
		checkLine: function(position,color){
			var $this = this;
			var direct = new Array();
			var num = 5;
			var isWin = false;
			//方向向量
			direct[0] = [0,1];	//下
			direct[1] = [0,-1];	//上
			direct[2] = [-1,0]; //左
			direct[3] = [1,0];  //右
			direct[4] = [-1,-1]; //左上
			direct[5] = [1,-1];	//右上
			direct[6] = [-1,1]; //左下
			direct[7] = [1,1];  //右下
			direct.forEach(function(d){
				var current = $this.matrix[position.x][position.y];
				var count = 0;
				for(var i=1;i<num;i++){
					var next = $this.matrix[position.x + d[0]*i][position.y + d[1]*i];
					if(current == next ){
						count = i;
					}else{
						return ;
					}
				}
				if(count == 4){
					isWin = true;
				}
			});
			return isWin;
		},
		setTurn: function(turn){
			var black = doc.getElementById('black');
			var white = doc.getElementById('white');
			if(turn == Chessman.BLACK){
				black.className = 'head black active';
				white.className = 'head white';
			}else{
				black.className = 'head black';
				white.className = 'head white active';
			}
		}
	}
	
	/**
	 * 玩家
	 */
	function Player(game,color,isAi){
		this.isAi = isAi;
		this.game = game;
		this.chessman = color;
		this.stepOver = false;
		if(color == Chessman.BLACK){
			this.color = "黑方";
		}
		if(color == Chessman.WHITE){
			this.color = "白方";
		}
		if(isAi){
			//创建AI程序	
		}
		
	}
	
	Player.prototype = {
		down: function(position){
			this.game.steps[this.chessman].push(position);
			var chessboard = this.game.chessboard;
			this.stepOver = chessboard.downCell(position,this.chessman);
			if(chessboard.checkLine(position,this.chessman)){
				this.game.notice(this,true);
			}
		},		
		admit: function(){
			this.game.notice(this,false);
		},
		regret: function(){
			var position = this.game.steps[this.chessman].pop();
			var chessboard = this.game.chessboard;
			chessboard.removeCell(position);
		}
	}
	

	/**
	 * 棋子
	 */
	var Chessman = {
		BLACK: 0,
		WHITE: 1,
		NULL: -1
	}
	
	window.Game = Game;
}(window));